using System;
using System.Configuration;
using System.Windows.Forms;

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Security;
using DevExpress.ExpressApp.Win;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
using DevExpress.ExpressApp.Workflow.Win;
using DevExpress.ExpressApp.Workflow;
using DevExpress.ExpressApp.Utils;
using WorkFlowDemo.Module.Activities;

namespace WorkFlowDemo.Win
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
#if EASYTEST
            DevExpress.ExpressApp.Win.EasyTest.EasyTestRemotingRegistration.Register();
#endif
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            EditModelPermission.AlwaysGranted = System.Diagnostics.Debugger.IsAttached;
            WorkFlowDemoWindowsFormsApplication winApplication = new WorkFlowDemoWindowsFormsApplication();
            // Refer to the http://documentation.devexpress.com/#Xaf/CustomDocument2680 help article for more details on how to provide a custom splash form.
            //winApplication.SplashScreen = new DevExpress.ExpressApp.Win.Utils.DXSplashScreen("YourSplashImage.png");
            if (ConfigurationManager.ConnectionStrings["ConnectionString"] != null)
            {
                winApplication.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            }
#if EASYTEST
            if(ConfigurationManager.ConnectionStrings["EasyTestConnectionString"] != null) {
                winApplication.ConnectionString = ConfigurationManager.ConnectionStrings["EasyTestConnectionString"].ConnectionString;
            }
#endif
            //winApplication.Modules.FindModule<WorkflowWindowsFormsModule>().QueryAvailableActivities += delegate(object sender, ActivitiesInformationEventArgs e)
            //{
            //    e.ActivitiesInformation.Add(new ActivityInformation(typeof(CreateTask), "Code Activities", "Create Task", ImageLoader.Instance.GetImageInfo("CreateTask").Image));
            //};

            WorkflowServerStarter starter = null;
            winApplication.LoggedOn += delegate(object sender, LogonEventArgs e)
            {
                if (starter == null)
                {
                    starter = new WorkflowServerStarter();
                    starter.OnCustomHandleException += starter_OnCustomHandleException;
                    starter.Start(winApplication.ConnectionString, winApplication.ApplicationName);
                }
            };


            try
            {
                winApplication.Setup();
                winApplication.Start();
            }
            catch (Exception e)
            {
                winApplication.HandleException(e);
            }
        }

        static void starter_OnCustomHandleException(object sender, ExceptionEventArgs e)
        {
            MessageBox.Show(e.Message);
        }
    }


    public class ExceptionEventArgs : EventArgs
    {
        public string Message { get; private set; }
        public ExceptionEventArgs (String message)
        {
            this.Message = message;
        }
    }

}
